package servlet.article;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.AType;
import entity.Article;
import service.ArticleService;

/**
 * Servlet implementation class ArticleEditServlet
 */
@WebServlet("/ArticleEditServlet")
public class ArticleEditServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ArticleEditServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 1、获取id
		String id = request.getParameter("id");
		// 2、获取对应的Article
		ArticleService service = new ArticleService();
		
		Article article = service.findById(id);
		// 转发
		request.setAttribute("article", article);
		
		List<AType> types = new ArrayList<>();
		AType aType = new AType();
		aType.setId("1");
		aType.setName("type1");
		
		AType aType2 = new AType();
		aType2.setId("2");
		aType2.setName("type2");
		
		types.add(aType);
		types.add(aType2);
		
		request.setAttribute("types", types);
		
		
		request.getRequestDispatcher("article/article_edit.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 接收article_edit.jsp页面的表单
		Article article = new Article();
		
		article.setId(request.getParameter("id"));
		article.setContent(request.getParameter("content"));
		
		// article集合的修改
		ArticleService service = new ArticleService();
		service.update(article);
		// 跳转
		response.sendRedirect("ArticleListServlet");
	}

}
